HOMEWORK 1

Settings Up

Loading Packages

# Loading Packages 

library(data.table)
library(lubridate)
library(tidyverse)
library(esquisse)
library(plyr)
library(ggplot2)
library(cowplot)
library(naniar) #for NA exploration
library(sp) #spatial data
library(ggmap) #for map
library(osmdata) #openstreetmap
library(reshape2)
library(plotly)

Loading Datas and Cleaning

Laptop_Sales_Data <- fread("DATA/LaptopSales_red.csv")

#is.data.table(Laptop_Sales_Data)

#summary(Laptop_Sales_Data)

#str(Laptop_Sales_Data)

gg_miss_var(Laptop_Sales_Data, show_pct = TRUE)

EX 3.4

a.Price Questions:

i. At What Price are the laptops actually selling ?

#### Histogram of the Retail Price of Computer In 2018

ggplot(Retail_Price_and_Dates) +
 aes(x = Retail.Price) +
 geom_histogram(bins = 30L, fill = "#1c6155") +
 labs(x = "Price", y = "Frequency", title = "Histogram of the Retail Price of Computer", subtitle = "In 2018") +
 theme_classic()

#### Boxplot of the Retail Price of Computer In 2018

ggplotly(
ggplot(Retail_Price_and_Dates) +
 aes(x = "", y = Retail.Price) +
 geom_boxplot(fill = "#1c6155") +
 labs(y = "Price", x="",
 title = "Boxplot of the Retail Price of Computer", subtitle = "In 2018") +
 theme_classic()
)
print(paste("Last Recorded Prices are", Actual_Price[1,1], "USD", "and", Actual_Price[2,1],"USD","on the same Day with a mean of",mean(Actual_Price$Retail.Price),"USD"))
## [1] "Last Recorded Prices are 406 USD and 530 USD on the same Day with a mean of 468 USD"

ii.Does price change with time?

# Retail Price By Month
RetailPricePlot1 <- ggplotly(
  ggplot(Retail_Price_and_Dates_Month) +
 aes(x = Date, y = Mean_Retail_Price) +
 geom_line(size = 1.1, 
 colour = "#1c6155") + geom_point() +
 labs(x = "Months", y = "Average Retail Pric", title = "Average Retail Price of Computer in 2018", 
 subtitle = "Aggregated by Month") +
 theme_classic()
 )

RetailPricePlot1
# Retail Price By Week
RetailPricePlot2 <- ggplotly(
  ggplot(Retail_Price_and_Dates_Week) +
 aes(x = Date, y = Mean_Retail_Price) +
 geom_line(size = 0.4, 
 colour = "#1c6155") + geom_point() +
 labs(x = "Weeks", y = "Average Retail Pric", title = "Average Retail Price of Computer in 2018", 
 subtitle = "Aggregated by Week") +
 theme_classic()
 )

RetailPricePlot2
# Retail Price By Day
RetailPricePlot3 <- ggplotly(
  ggplot(Retail_Price_and_Dates_Day) +
 aes(x = Date, y = Mean_Retail_Price) +
 geom_line(size = 0.2, 
 colour = "#1c6155")  +
 labs(x = "Days", y = "Average Retail Price", title = "Average Retail Price of Computer in 2018", 
 subtitle = "Aggregated by Day") + 
 theme_classic()
 )

RetailPricePlot3
# Retail Price By Week Day

RetailPricePlot4 <- ggplotly(
  ggplot(Retail_Price_and_Dates_WeekDay) +
 aes(x = Weekday, y = Mean_Retail_Price) +
 geom_col(fill = "#1c6155") +
 labs(x = "Weekdays", y = "Average Retail Price", title = "Average Retail Price during Weekdays", subtitle = "In 2018") + 
 theme_classic() + coord_cartesian(ylim = c(505, 510))
)

RetailPricePlot4

iii. Are prices consistent across retail outlets?

#### Boxplot Across Retail Outlets

ggplotly(
ggplot(Retail_Price_Outlets_Date) +
 aes(x = Store.Postcode, y = Retail.Price) +
 geom_boxplot(fill = "#1c6155") +
 labs(x = "Stores Postcode", y = "Price", title = "Boxplot Of The Retail Price Across Stores", subtitle = "In 2018") +
 theme_classic() + scale_x_discrete(guide = guide_axis(n.dodge = 1)) + theme(axis.text.x=element_text(size=rel(1), angle=90))
)
#### Plot of the Monthly Retail Price per Stores

ggplot(Retail_Price_Outlets_Date_Month) +
 aes(x = Floor.Date, y = Mean_Retail_Price, colour = Store.Postcode) +
 geom_line(size = 0.5) +
 scale_color_hue(direction = 1) +
 labs(x = "Month", y = "Price", title = "Retail Price Across Months and Grouped by Stores", 
 subtitle = "In 2018") +
 theme_classic() 

iv. How does price change with configuration?

#### Plot Of The Retail Price per Configuration

ggplot(Retail_Price_Configuration) +
 aes(x = Configuration, y = Retail.Price) +
 geom_point(shape = "circle", size = 0.6) +
 scale_color_gradient() +
 labs(y = "Retail Price", title = "Retail Price and Configuration ", 
 subtitle = "In 2018") + geom_smooth() + theme_classic()

b.Location Questions

i. Where are the stores and customers locatd?

ii. Which stores are selling the most?

#### Plot Transactions per Stores

ggplot(Sales_Stores) +
 aes(x = reorder(Store.Postcode,-N), y = N) +
 geom_col(fill = "#1c6155") +
 labs(x = "Stores", 
 y = "Number Of Transactions", title = "Number of Transactions per Store", subtitle = "In 2018") +
 theme_classic()  + geom_text(aes(label = N), vjust = 1.5, hjust=1.1, colour = "white", angle=90,size=3) + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

# Plot Revenues per Stores
ggplot(Sales_Stores_2) +
 aes(x = reorder(Store.Postcode,-Revenues), y = Revenues) +
 geom_col(fill = "#1c6155") +
 labs(x = "Store Postcode", 
 y = "Revenues", title = "Revenues per Stores", subtitle = "In 2018") + geom_text(aes(label = Revenues), vjust = 1.5, hjust=1.1, colour = "white", angle=90,size=3) + 
 theme_classic()+theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

iii. How far stores are selling the most?

iv. How far stores are selling the most? - Alternative

c.Revenue Questions

i. How do the sales volume in each store relate to Acell’s revenues?

ggplot(Sales_Stores_Revenues) +
 aes(x = reorder(Store.Postcode,-Percentage_Revenue), y = Percentage_Revenue) +
 geom_col(fill = "#1c6155") +
 labs(x = "Store Postcode", y = "% of Total Revenues", title = "Revenues Contribution per Stores", 
 subtitle = "In 2018") + 
 theme_classic()+theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +geom_text(aes(label = round(Percentage_Revenue,1)), vjust = 1.5, hjust=1.1, colour = "white", angle=90,size=3)

ii. How does this relationship depend on the configuration?

d.Configuration Questions

i. What are the details of each configuration? How does this relate to price?

ii. Do all stores sell all configurations?

ggplot(Stores_Details) +
 aes(x = Configuration) +
 geom_histogram(bins = 30L, fill = "#112446") +
 labs(x = "Stores ", 
 y = "Configurations Count", title = "Each Configurations per Stores", subtitle = "In 2018") +
 theme_classic() +
 facet_wrap(vars(Store.Postcode), scales = "free")